<?php
/**
 * SiteController.php
 *
 * @author: antonio ramirez <antonio@clevertech.biz>
 * Date: 7/23/12
 * Time: 12:25 AM
 */
class SiteController extends Controller {

	public function accessRules() {
		return array();
	}

	/**
	 * Declares class-based actions.
	 * @return array
	 */
	public function actions() {
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha' => array(
				'class' => 'CCaptchaAction',
				'backColor' => 0xFFFFFF,
			),
		);
	}

	/* open on startup */
	public function actionIndex() {
		$this->render('index');
	}

	/**
	 * This is the action to handle external exceptions.
	 */
	public function actionError() {
		if ($error = Yii::app()->errorHandler->error) {
			if (Yii::app()->request->isAjaxRequest)
				echo $error['message'];
			else
				$this->render('error', $error);
		}
	}

	/**
	 * Displays the login page
	 */
	public function actionLogin() {
		$model = new LoginForm;

		// if it is ajax validation request
		if (isset($_POST['ajax']) && $_POST['ajax'] === 'login-form') {
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}

		// collect user input data
		if (isset($_POST['LoginForm'])) {
			$model->attributes = $_POST['LoginForm'];
			// validate user input and redirect to the previous page if valid
			if ($model->validate() && $model->login())
				$this->redirect(ApplicationConfig::getURL('', 'site', 'myprofile'));
		}
		// display the login form
		$this->render('login', array('model' => $model));
	}

	/**
	 * Logs out the current user and redirect to homepage.
	 */
	public function actionLogout() {
		Yii::app()->user->logout();
		$this->redirect(Yii::app()->homeUrl);
	}
	
	public function actionRegister()
	{
		$model = new RegisterUser();
		
	 
		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);
		
		if(isset($_POST['RegisterUser']))
		{
    		$model->attributes=$_POST['RegisterUser'];
    		$model->password = md5($_POST['RegisterUser']['password']);	
    		$model->confirm_password = md5($_POST['RegisterUser']['confirm_password']);
    		$model->user_name = $_POST['RegisterUser']['email_id'];
			if($model->validate())
	       {
	       		$code = $_POST['code'];
	       		if (!empty($code) && ($code == "VDKZC3" || $code=="UGLPX9"))
	       		{
	       		$model->save();
		       	$model1=new UserPoints;
				$model1->user_id=$model->user_id;
		       	$model1->points=100;
	      		 $model1->save();
	       		}
	       		else 
	       		{
	       			$model->addError('password', 'Invalid security code.');
	       		}
	        }
	        $errors = $model->getErrors();
	        if (!empty($errors))
	        {
	        	foreach ($errors as $error)
	        	{
	        		echo $error[0];
	        		break;
	        	}
	        }
	        else 
	        echo "Thank you for the registration. Please login.";
	        exit;
		}
		
		$this->render('register',array('model'=>$model));
	}


}                                   